﻿using System.Xml.Linq;

namespace Songhay.OpenXml
{
    using Songhay.Xml;

    /// <summary>
    /// Static members for Open XML.
    /// </summary>
    public static partial class OpenXmlUtility
    {
        /// <summary>
        /// Renders the empty element.
        /// </summary>
        /// <param name="e">The e.</param>
        /// <remarks>
        /// Note that an element that contains a start
        /// and end tag with no content between the tags
        /// is not considered to be an empty element.
        /// It has content with no length.
        /// Only an element that contains only a start tag,
        /// and is expressed as a terminated empty element,
        /// is considered to be empty.
        /// (http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.isempty.aspx)
        /// </remarks>
        public static XElement RenderEmptyElement(XElement e)
        {
            if(e == null) return e;

            switch(e.Name.LocalName)
            {
                case "p":
                case "script":
                case "td":
                    if(e.IsEmpty || string.IsNullOrWhiteSpace(e.Value))
                    {
                        return new XElement(e.Name,
                            XObjectUtility.XTextNonBreakingSpace);
                    }
                    break;
                default:
                    break;
            }
            return e;
        }
    }
}
